import { Body, Controller, Get, HttpException, Post, Res, Response, Request, HttpStatus, Query, UseInterceptors } from '@nestjs/common';
import { Tags } from './tags.entity';
import { TagsService } from './tags.service';
import { UserFuncController } from '../user/userFunc.controller';
import { UserService } from '../user/user.service';
import { apibefore } from 'src/router';
import utils from 'src/common/utils';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';

@Controller(apibefore('/tags'))
export class TagsController extends UserFuncController {
  constructor(private readonly service: TagsService,
    private readonly userService: UserService
    ) {
      super(userService)
    }


  @Post('/create_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async create_auth(@Body() data: Tags, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let isExists = await this.service.findIsExists(data.tag)
    if(isExists){
      return new HttpException('标签已存在', HttpStatus.INTERNAL_SERVER_ERROR)
    }
    
    let saveData = new Tags();
    saveData.tag = data.tag
    saveData.create_time = new Date()
    saveData.parentId = data.parentId
    saveData.user_id = req.headers.token
    return await this.service.create(saveData);
  }


  @Post('/create')
  async create(@Body() data: Tags, @Request() req) {
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let isExists = await this.service.findIsExists(data.tag)
    if(isExists){
      return new HttpException('标签已存在', HttpStatus.INTERNAL_SERVER_ERROR)
    }
    
    let saveData = new Tags();
    saveData.tag = data.tag
    saveData.create_time = new Date()
    saveData.parentId = data.parentId
    saveData.user_id = req.headers.token
    return await this.service.create(saveData);
  }

  
  @Get('/findAll_By_User_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_By_User_auth(@Query() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const page = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_By_User(req.headers.token, page.skip, page.take, params.keyword)
  }

  @Get('/findAll_By_User')
  async findAll_By_User(@Query() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    const page = utils.getLimit(params.pageSize, params.pageIndex)
    return await this.service.findAll_By_User(req.headers.token, page.skip, page.take, params.keyword)
  }

  @Get('/findAll_By_User_NoPage')
  async findAll_By_User_NoPage(@Query() params, @Request() req){
    //查询用户的wx_user
    let findUser = await this.findWxUser(req.headers.token)
    if(findUser.error){
      return new HttpException(findUser.error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return await this.service.findAll_By_User_NoPage()
  }

  @Get('/findAll_By_ParentId')
  async findAll_By_ParentId(@Query() params){
    return await this.service.findAll_By_ParentId(params.parentId, params.tag_childs)
  }

  @Post('/update')
  async update(@Body() data: Tags){
    let isExists = await this.service.findIsExists(data.tag)
    if(isExists){
      return new HttpException('标签已存在', HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return await this.service.update(data.id, data.tag)
  }

  @Post('/remove')
  async remove(@Body() data: Tags){
    let error = await this.service.remove(data.id)
    if(error){
      return new HttpException(error, HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return true
  }
}
